#include "cmd.h"
#include "stdio.h"
#include "string.h"


extern cmd_tbl_t * __lx_mcu_cmd_start;
extern cmd_tbl_t * __lx_mcu_cmd_end ;

int _do_help(cmd_tbl_t *cmd_start, int cmd_items, cmd_tbl_t *cmdtp, 
             int argc, char * const argv[])
{
	int i = 0;
	int rcode = 0;
	
	if (1 == argc) /* show list of commands */
	{
		/* print short help (usage) */
		for (i = 0; i < cmd_items; i++)
		{
			const char *usage = cmd_start[i].usage;
			
			if (NULL == usage)
				continue;
			
			printf("%-*s- %s\r\n", SYS_HELP_CMD_WIDTH, 
				cmd_start[i].name, usage);
		}
		
		return 0;
	}
	
	/*
	 * command help (long version)
	 *
	*/
	for (i = 1; i < argc; ++i)
	{
		if ((cmdtp = find_cmd_tbl(argv[i], cmd_start, cmd_items )) != NULL)
		{
			rcode |= cmd_usage(cmdtp);
		}
		else
		{
			printf("Unknow command '%s' - try help"
				" without arguments for list of all"
				" known commands \r\n\r\n", argv[i]);
			rcode = 1;
		}
	}
	
	return rcode;
}

int do_help(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{
	return _do_help(__lx_mcu_cmd_start,
			__lx_mcu_cmd_end - __lx_mcu_cmd_start,
			cmdtp, argc, argv);
}

LYNXI_MCU_CMD(
	help,	MAX_CMD_ARG_NUM, 1	,do_help,
	"Print out help menu.",
	"help [cmd]"
);
